package com.projectName.www.service;


import com.projectName.www.po.orders;
import com.projectName.www.util.String_util;
import com.projectName.www.util.find;
import com.projectName.www.dao.S_J_orders_dao;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.util.Vector;

/**
     * @author 86132
     *
     */
    public class S_J_orders extends JInternalFrame {
        private JTable table_0;
        private JTable table_1;
        private JTextField money;
    private int i;//选中的行
    private orders r=new orders();
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        S_J_orders frame = new S_J_orders();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        /**
         * Create the frame.
         */
        public S_J_orders() throws Exception {
            setMaximizable(true);
            setClosable(true);
            setBounds(100, 100,750, 525);

            JLabel lblNewLabel = new JLabel("未处理的订单");
            lblNewLabel.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 18));

            JLabel lblNewLabel_1 = new JLabel("已处理的订单");
            lblNewLabel_1.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 18));

            JScrollPane scrollPane = new JScrollPane();

            JScrollPane scrollPane_1 = new JScrollPane();

            JButton yes = new JButton("选中接单");
            yes.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 25));

            JButton no = new JButton("选中退回");
            no.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 25));

            JLabel lblNewLabel_2 = new JLabel("收益：");
            lblNewLabel_2.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 27));

            money = new JTextField();
            money.setEditable(false);
            money.setColumns(10);
            GroupLayout groupLayout = new GroupLayout(getContentPane());
            groupLayout.setHorizontalGroup(
                    groupLayout.createParallelGroup(Alignment.LEADING)
                            .addGroup(groupLayout.createSequentialGroup()
                                    .addGap(98)
                                    .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 168, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(ComponentPlacement.RELATED, 180, Short.MAX_VALUE)
                                    .addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 168, GroupLayout.PREFERRED_SIZE)
                                    .addGap(128))
                            .addGroup(groupLayout.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 358, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(ComponentPlacement.RELATED)
                                    .addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 358, GroupLayout.PREFERRED_SIZE)
                                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(groupLayout.createSequentialGroup()
                                    .addGap(27)
                                    .addComponent(yes, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
                                    .addGap(18)
                                    .addComponent(no, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
                                    .addGap(41)
                                    .addComponent(lblNewLabel_2)
                                    .addPreferredGap(ComponentPlacement.UNRELATED)
                                    .addComponent(money, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
                                    .addContainerGap(179, Short.MAX_VALUE))
            );
            groupLayout.setVerticalGroup(
                    groupLayout.createParallelGroup(Alignment.LEADING)
                            .addGroup(groupLayout.createSequentialGroup()
                                    .addContainerGap()
                                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                            .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
                                            .addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE))
                                    .addGap(18)
                                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                            .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 304, GroupLayout.PREFERRED_SIZE)
                                            .addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 304, GroupLayout.PREFERRED_SIZE))
                                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                            .addGroup(groupLayout.createSequentialGroup()
                                                    .addPreferredGap(ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                                                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                                            .addComponent(yes, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                                                            .addComponent(no, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)))
                                            .addGroup(groupLayout.createSequentialGroup()
                                                    .addGap(51)
                                                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                                            .addComponent(money, GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
                                                            .addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE))))
                                    .addGap(42))
            );

            table_1 = new JTable();
            table_1.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                    },
                    new String[] {
                            "\u83DC\u540D", "\u6570\u91CF", "\u91D1\u989D", "\u5BA2\u6237"
                    }
            ) {
                boolean[] columnEditables = new boolean[] {
                        false, false, false, false
                };
                public boolean isCellEditable(int row, int column) {
                    return columnEditables[column];
                }
            });
            scrollPane_1.setViewportView(table_1);

            table_0 = new JTable();
            table_0.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                    },
                    new String[] {
                            "\u83DC\u540D", "\u6570\u91CF", "\u91D1\u989D", "\u5BA2\u6237"
                    }
            ) {
                boolean[] columnEditables = new boolean[] {
                        false, false, false, false
                };
                public boolean isCellEditable(int row, int column) {
                    return columnEditables[column];
                }
            });
            scrollPane.setViewportView(table_0);
            getContentPane().setLayout(groupLayout);
            filltable();
            fillmoney();

            yes.addActionListener(e->
                    {
                        try {

                            yesORno(2);
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                    }
            );
            no.addActionListener(e->
            {
                try {
                   yesORno(3);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            });
            table_0.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    i=table_0.getSelectedRow();
                    System.out.println(i);
                }
            });



        }
//接单&&退回
    private void yesORno(int k) throws Exception {
        r.setBool_shop(1);
        r.setBool_buyer(1);
        r.setBuyers((String) table_0.getValueAt(i,3));
        r.setBusiness(find.name(OpenBusiness.username));
        r.setDishename((String) table_0.getValueAt(i,0));
        r.setNum(String_util.num((String) table_0.getValueAt(i,1)));
        r.setMoney(String_util.num((String) table_0.getValueAt(i,2)));
        boolean b = S_J_orders_dao.yesORno(r, k);
        if(b)
        {
            JOptionPane.showMessageDialog(null,"操作成功");
            filltable();
            fillmoney();


        }else JOptionPane.showMessageDialog(null,"操作失败");

    }

    //填充表格
        public void filltable() throws Exception {

            DefaultTableModel model=(DefaultTableModel)table_0.getModel();
            model.setRowCount(0);
            ResultSet r=S_J_orders_dao.filltable(0);
            while(r.next())
            {
                Vector v0 = new Vector();
                v0.add(r.getString("dishename"));
                v0.add(r.getString("num"));
                v0.add(r.getString("money"));
                v0.add(r.getString("Buyers"));
                model.addRow(v0);
            }
            DefaultTableModel model1=(DefaultTableModel)table_1.getModel();
            model1.setRowCount(0);
            ResultSet r1=S_J_orders_dao.filltable(1);
            while(r1.next())
            {
                Vector v1 = new Vector();
                v1.add(r1.getString("dishename"));
                v1.add(r1.getString("num"));
                v1.add(r1.getString("money"));
                v1.add(r1.getString("Buyers"));
                model1.addRow(v1);
            }

        }
        public void  fillmoney() throws Exception {
            money.setText(S_J_orders_dao.money());
        }
}
